<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

if(get('page')!=''){
    $pageid = (int) get('page');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$pageid}'")==0){
        JFX::addError('Invalid page to delete');
        JFX::redirect($CONFIG->adminUrl);
    }
}

$THEME->addBreadcrumb($this->adminUrl.'/delete-page/', $this->lang('ap_title_delete-page'));


// have a valid page to delete


if(Antz_IntelliForm::submitted('delete-page')){
    // lets delete this page
    $parentid = (int) get('parent');
    if((int) post('recursive', 0) !== 1){
        $DB->update($CONFIG->dbprefix.'pages', array('parent_id'=>$parentid), "parent_id = '{$pageid}'");
        JFX_DeletePage_DeletePage($pageid, $USER->workspace, false);
    }else{
        JFX_DeletePage_DeletePage($pageid, $USER->workspace, true);
    }

    JFX::addSuccess('The page has been deleted!');
    JFX::redirect($CONFIG->adminUrl);

}


if(get('parent')!=''){
    $parentid = (int) get('parent');
    if($DB->countRows($CONFIG->dbprefix.'pages', "id = '{$parentid}'")==0){
        JFX::addError('Invalid new parent');
        JFX::redirect($CONFIG->adminUrl.'/delete-page/?page='.$pageid);
    }
}else{
    // make them choose the new parent


    $VIEW->addJs('
    $(document).ready(function(){
        $("#jfxcore-delete-page-tree").treeview({
            persist: "location",
            collapsed: true,
            unique: true
        });
    });
    ');




    $pagesRecursive = JFX::getPageTree(0, $USER->workspace);

    $treeFrom = '<ul id="jfxcore-delete-page-tree">'.JFX_DeletePage_MakeTree($pagesRecursive, $USER->workspace, $pageid).'</ul>';

    $formFields = array(
        array(
            'type' => 'checkbox',
            'name' => 'recursive',
            'value' => 1,
            'label' => 'I\'m sure',
            'checked' => true
        ),
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'value' => 'Delete',
            'label' => '&nbsp;'
        )
    );
    $recursiveForm = JFX::makeRapidForm($formFields, 'Recursive Delete', 'delete-page');

    $SMARTY->assign('recursiveDeleteForm', $recursiveForm->render());
    $SMARTY->assign('treeFrom', $treeFrom);
    $SMARTY->assign('deletePageTitle', $LANG->getContent('core', 'page_title', $pageid));



    $VIEW->addTemplate('main', 'admin/delete-page.tpl');

    return;
}

// have a valid page and parent







$formFields = array(
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'value' => 'Confirm Delete',
        'label' => '&nbsp;'
    )
);

$pageTitle = $LANG->getContent('core', 'page_title', $pageid);
$parentTitle = $LANG->getContent('core', 'page_title', $parentid);

echo JFX::makeRapidForm($formFields, 'Confirm deleting of '.$pageTitle.' and moving subpages into '.$parentTitle, 'delete-page');








function JFX_DeletePage_MakeTree($arr, $workspaceid, $pageid){
    $CONFIG = JFX::registry('config');
    $LANG = JFX::registry('lang');
    $returnString = '';
    foreach($arr as $k=>$v){

        $link = '<a href="'.$CONFIG->adminUrl.'/delete-page/?page='.$pageid.'&amp;parent='.$v['id'].'">'.$LANG->getContent('core', 'page_title', $v['id'], '', $workspaceid).'</a>';
        if(isset($v['subpages']) && count($v['subpages'])>0){
            $subpages = '<ul>'.JFX_DeletePage_MakeTree($v['subpages'], $workspaceid, $pageid).'</ul>';
        }else{
            $subpages = '';
        }
        $returnString .= '<li>'.$link.$subpages.'</li>';
    }

    return $returnString;
}








function JFX_DeletePage_DeletePage($pageid, $workspaceid, $recursive=false){
    $DB = JFX::registry('db');
    $CONFIG = JFX::registry('config');
    $LANG = JFX::registry('lang');

    $block = new JFX_Block;
    $blocks = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE page_id = '{$pageid}'");
    foreach($blocks as $k=>$v){
        $block->loadById($v['id'], $v['workspace_id'], true, true);
        $block->delete();
    }

    $DB->delete($CONFIG->dbprefix.'pages', "id = '{$pageid}'");

    $LANG->deleteContent('core', 'page_title', $pageid, '*', '*');
    $LANG->deleteContent('core', 'page_menu', $pageid, '*', '*');
    $LANG->deleteContent('core', 'page_content', $pageid, '*', '*');

    $params = array(
        'page_id' => $pageid,
        'workspace_id' => $workspaceid
    );
    JFX::hook('JFX_Core.deletePage', $params);

    if($recursive === true){
        $subpages = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}pages WHERE parent_id = '{$pageid}' AND workspace_id = '{$workspaceid}'");
        foreach($subpages as $k=>$v){
            JFX_DeletePage_DeletePage($v['id'], $workspaceid, $recursive);
        }
    }

}



